package helpers;

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;

public class StringHelper {
    public static String cutString(String description, int count) {
        String[] str = description.split(" ");
        int max = str.length;
        if (max >= count) {
            max = count;
        }
        description = "";
        
        for (int i = 0; i < max; i++) {
            description += str[i] + " ";
        }
        return description;
    }
    
    public static String getAlias(String str) {
        str = StringHelper.replaceVietnamese(str);
        str = StringHelper.replace(str);
        return str;
    }
    
    public static String replace(String str) {
        str = str.replaceAll("[\\W]+", "-");
        str = str.replaceAll("^-", "");
        str = str.replaceAll("-$", "");
        str = str.toLowerCase();
        return str;
    }

    public static String replaceVietnamese(String str) {
		String[] aArray = {"áº¥", "áº§", "áº©", "áº«", "áº­", "áº¤", "áº¦", "áº¨", "áºª", "áº¬", "áº¯", "áº±", "áº³", "áºµ", "áº·",
				"áº®", "áº°", "áº²", "áº´", "áº¶", "Ã¡", "Ã ", "áº£", "Ã£", "áº¡", "Ã¢", "Äƒ",
				"Ã�", "Ã€", "áº¢", "Ãƒ", "áº ", "Ã‚", "Ä‚" };
		String[] eArray = { "áº¿", "á»�", "á»ƒ", "á»…", "á»‡", "áº¾", "á»€", "á»‚", "á»„", "á»†",
				"Ã©", "Ã¨", "áº»", "áº½", "áº¹", "Ãª", "Ã‰", "Ãˆ", "áºº", "áº¼", "áº¸", "ÃŠ" };
		String[] iArray = { "Ã­", "Ã¬", "á»‰", "Ä©", "á»‹", "Ã�", "ÃŒ", "á»ˆ", "Ä¨", "á»Š" };
		String[] oArray = { "á»‘", "á»“", "á»•", "á»—", "á»™", "á»�", "á»’", "á»”", "Ã”", "á»˜",
				"á»›", "á»�", "á»Ÿ", "á»¡", "á»£", "á»š", "á»œ", "á»ž", "á» ", "á»¢", "Ã³", "Ã²",
				"á»�", "Ãµ", "á»�", "Ã´", "Æ¡", "Ã“", "Ã’", "á»Ž", "Ã•", "á»Œ", "Ã”", "Æ " };
		String[] uArray = { "á»©", "á»«", "á»­", "á»¯", "á»±", "á»¨", "á»ª", "á»¬", "á»®", "á»°",
				"Ãº", "Ã¹", "á»§", "Å©", "á»¥", "Æ°", "Ãš", "Ã™", "á»¦", "Å¨", "á»¤", "Æ¯" };
		String[] yArray = { "Ã½", "á»³", "á»·", "á»¹", "á»µ", "Ã�", "á»²", "á»¶", "á»¸", "á»´" };
		String[] dArray = { "Ä‘", "Ä�" };
		for (String item : aArray) {
			str = str.replaceAll(item, "a");
		}
		for (String item : eArray) {
			str = str.replaceAll(item, "e");
		}
		for (String item : iArray) {
			str = str.replaceAll(item, "i");
		}
		for (String item : oArray) {
			str = str.replaceAll(item, "o");
		}
		for (String item : uArray) {
			str = str.replaceAll(item, "u");
		}
		for (String item : yArray) {
			str = str.replaceAll(item, "y");
		}
		for (String item : dArray) {
			str = str.replaceAll(item, "d");
		}
		str = str.replaceAll("[\\W]+", "-");
		str = str.replaceAll("^-", "");
		str = str.replaceAll("-$", "");
		str = str.toLowerCase();
		return str;

	}

    public static String tag(String str) {
        str = str.replaceAll("\\#", "Sharp");
        str = str.replaceAll("\\+", "Plus");
        str = str.replaceAll("[^.\\w]+", "-");
        str = str.replaceAll("^-", "");
        str = str.replaceAll("-$", "");
        str = str.toLowerCase();
        return str;
    }

    public static String remove(String str) {
        str = str.replaceAll("[\'\"]+", "&quot;");
        return str;
    }

    public static boolean StringIsNullOrEmpty(String string) {
        if (string == null || string.equals("")) {
            return true;
        }
        return false;
    }
    
    public static ArrayList<String> findString(String regex, String input) {
        ArrayList<String> result = new ArrayList<String>();
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(input); // get a matcher object
         while(m.find()) {
           result.add(input.substring(m.start(), m.end()));
        }
        return result;
    }
    
    public static String html2text(String html) {
        return Jsoup.parse(html).text();
    }
    
    public static String text2UTF8(String str) {
        byte[] utf8bytes = str.getBytes();
        return new String(utf8bytes,Charset.forName("UTF-8"));
    }
    
    public static String arrayToString(String[] a, String separator) {
        if (a == null || separator == null) {
            return null;
        }
        StringBuilder result = new StringBuilder();
        if (a.length > 0) {
            result.append(a[0]);
            for (int i=1; i < a.length; i++) {
                result.append(separator);
                result.append(a[i]);
            }
        }
        return result.toString();
   }
   
	public static String MapStringToString(Map<String, String> map, String separator) {
		StringBuilder result = new StringBuilder();
	    int i = 0;
		for (Map.Entry<String, String> entry : map.entrySet()) {
			if (i > 0) {
				result.append(separator);
			}
			result.append(entry.getKey() + "|" + entry.getValue());
			i++;
		}
		return result.toString();
	}
    
	public static String arrayListToString(ArrayList<String> a, String separator) {
      StringBuilder result = new StringBuilder();
      if (a.size() > 0) {
          result.append(a.get(0));
          for (int i = 1; i < a.size(); i++) {
              result.append(separator);
              result.append(a.get(i));
          }
      }
      return result.toString();
  }
  
  public static String listToString(List<String> str, String separator) {
      StringBuilder result = new StringBuilder();
      if (str.size() > 0) {
          result.append(str.get(0));
          for (int i = 1; i < str.size(); i++) {
              result.append(separator);
              result.append(str.get(i));
          }
      }
      return result.toString();
  }
  
  public static String listToString(List<String> str, String separator, int count) {
      StringBuilder result = new StringBuilder();
      if (str.size() > 0) {
          result.append(str.get(0));
          for (int i = 1; i < str.size(); i++) {
        	  if (i > count) break;
              result.append(separator);
              result.append(str.get(i));
          }
      }
      return result.toString();
  }
  
  public static String md5(String str) {
	try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());

        byte byteData[] = md.digest();

        //convert the byte to hex format method 1
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
           sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
        }
        str = sb.toString();
    } catch (Exception e) {}
    return str;
  }
  
  public static String cleanContent(String str) {
		str = str.replaceAll(
				"(class|id)[ ]*[=]{1}[ ]*[\\'\\\"]{1}[^\\'\\\"]+[\\'\\\"]{1}",
				"");
		str = str.replaceAll("<script[^<]+</script[ ]*>", "");
		return str;
  }
  
  public static String replateContent(String content, Map<String, String> keys) {
	  
	  for(Map.Entry<String,String> entry : keys.entrySet()) {
		  content = content.replace(entry.getKey(), entry.getValue());
	  }
	  
	  return content;
  }
}
